package problem24;

//24.两两交换链表中的节点
//https://leetcode.cn/problems/swap-nodes-in-pairs/
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode newHead = new ListNode();
        newHead.next = head;
        ListNode cur1 = newHead, cur2 = newHead.next;
        while(cur2 != null && cur2.next != null) {
            ListNode next = cur2.next;
            cur2.next = next.next;
            next.next = cur2;
            cur1.next = next;
            cur1 = cur2;
            cur2 = cur2.next;
        }
        return newHead.next;
    }
}